package com.app.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Point;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.app.util.Utils_Log;
import com.app.util.cHighTimer;

import lib.common.R;


public class SurfaceViewDymic extends SurfaceView implements SurfaceHolder.Callback2, Runnable
{

	private static String TAG = SurfaceViewDymic.class.getSimpleName();

	private Context mContext;
	private SurfaceHolder mHolder;

	private int width;
	private int height;


	private Paint mPaintLine;
	private Point point_line_start ;
	private Point point_line_end ;
	private int mColorLine = Color.BLACK;

	private cHighTimer mHighTimer = new cHighTimer();
	private boolean bRun = false;



	///*
	public SurfaceViewDymic(Context context, AttributeSet set){
		super(context, set);
		
		init(context, set, 0);
		
	}
	//*/


	private void init(Context context, AttributeSet attrs, int defStyle) {
		// Load attributes


		mContext = context;


		mHolder = this.getHolder();
		mHolder.addCallback(this);


		if (null != attrs) {
			TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.MyCircleSurfaceView);

			if (ta != null) {

				ta.recycle();
			}
		} else {


		}


	}
	
	


	
	void initAnimation(){

		int x = getLeft();
		int y = getTop();


		mPaintLine = new Paint();
		mPaintLine.setColor( mColorLine );
		mPaintLine.setStrokeWidth(5);

		point_line_start = new Point(x,y);
		point_line_end = new Point(x+width, y);

		//启动计时器
		mHighTimer.start_counter();
	 
	}
	

	@Override
	public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void surfaceCreated(SurfaceHolder arg0) {
		// TODO Auto-generated method stub

		 width = getWidth();
		 height = getHeight();

		initAnimation();

		bRun = true;
		new Thread(this).start();
		
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder arg0) {
		// TODO Auto-generated method stub
		bRun = false;
	}

	@Override
	public void surfaceRedrawNeeded(SurfaceHolder arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		
		while(bRun){
			float elapsed = mHighTimer.get_elapsed_time();
			if( mHighTimer.controlFPS_Return(elapsed, 33) ){
				synchronized (mHolder) {
					Update();
					Draw();
				}	
			}
			
		}
		
		
	}	
	
	

	int delta_y = 0;
	void Update(){

		/*
		// 外环最大扩展是50的3倍；不断扩展不断淡化
		if(mRadiusRing > mRadius*mRectRingScale || alpha < 0x10){
			mRadiusRing = mRadius;
			alpha = 0xff;
		}
		else{
			mRadiusRing+=1;
			alpha -=5;
		}


		// 外环的包围框，用于调试使用
    	mRectRing.left =mPointCenter.x-mRadiusRing;
    	mRectRing.top =mPointCenter.y-mRadiusRing;
    	mRectRing.right =mPointCenter.x+mRadiusRing;
    	mRectRing.bottom =mPointCenter.y+mRadiusRing;
    	//Utils_Log.d(TAG, "mRectRing: "+mRectRing.toString() );

		mPaintRing.setAlpha(alpha);
		*/

		if(point_line_start.y>getBottom()){
			int y  = getTop();
			point_line_start.y = y;
			point_line_end.y = y;
		}

		point_line_start.offset(0, 1);
		point_line_end.offset(0, 1);


		Utils_Log.d(TAG, "point_line_start: " + point_line_start.toString());

	}
	
	
	
	void Draw(){
		

		Canvas canvas = mHolder.lockCanvas();
		if(canvas == null)
			return;

		//System.out.println("Draw");


		// 绘制背景颜色
		//canvas.drawColor(Color.parseColor("#aadddddd"));
		canvas.drawColor(Color.TRANSPARENT);

		canvas.drawCircle(getWidth()/2,getHeight()/2, 30, mPaintLine);


		canvas.drawLine(point_line_start.x, point_line_start.y, point_line_end.x, point_line_end.y, mPaintLine);


    	// 绘制中心
		//canvas.drawCircle(mPointCenter.x, mPointCenter.y, mRadius, mPaintCircle);

		// 绘制扩散的环
		//canvas.drawOval(mRectRing, mPaintRing);
    	
		
		mHolder.unlockCanvasAndPost(canvas);
		
		
	}







};

